//
//  RBTableViewDefine.h
//  RBDemoProj
//
//  Created by Lan on 2018/2/5.
//  Copyright © 2018年 SummerTea. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "RBGroupModel.h"
#import "RBCellModel.h"
#import "RBBinder.h"

#pragma mark - 忽略自动高度预估

#define kRBIgnoreTableViewEstimatedHeightAdjust(tableView) tableView.estimatedRowHeight = 0; \
tableView.estimatedSectionFooterHeight = 0;\
tableView.estimatedSectionHeaderHeight = 0;\

#pragma mark - 数据源&代理

#define kRBTableViewDataSourceAndDelegate(dataSource) - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView\
{\
    return dataSource.count;\
}\
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section\
{\
    RBGroupModel *group = dataSource[section];\
    return group.cellModels.count;\
}\
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath\
{\
    RBGroupModel *group = dataSource[indexPath.section];\
    RBCellModel *cellModel = group.cellModels[indexPath.row];\
    cellModel.indexPath = indexPath;\
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellModel.reuseCellIdentifier];\
    if (!cell)\
    {\
        NSString *path = [[NSBundle mainBundle] pathForResource:cellModel.reuseCellIdentifier ofType:@"nib"];\
        if (path.length)\
        {\
            UINib *nib = [UINib nibWithNibName:cellModel.reuseCellIdentifier bundle:nil];\
            [tableView registerNib:nib forCellReuseIdentifier:cellModel.reuseCellIdentifier];\
        }\
        else\
        {\
            [tableView registerClass:NSClassFromString(cellModel.reuseCellIdentifier) forCellReuseIdentifier:cellModel.reuseCellIdentifier];\
        }\
        cell = [tableView dequeueReusableCellWithIdentifier:cellModel.reuseCellIdentifier];\
    }\
    [RBBinder bindCell:cell withCellModel:cellModel];\
    return cell;\
}\
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath\
{\
    RBGroupModel *group = dataSource[indexPath.section];\
    RBCellModel *cellModel = group.cellModels[indexPath.row];\
    return cellModel.cellHeight;\
}\
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath\
{\
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{\
        [tableView deselectRowAtIndexPath:indexPath animated:YES];\
    });\
    RBGroupModel *group = dataSource[indexPath.section];\
    RBCellModel *cellModel = group.cellModels[indexPath.row];\
    if (cellModel.cellSelectBlock)\
    {\
        cellModel.cellSelectBlock(indexPath);\
    }\
}\
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section\
{\
    RBGroupModel *group = dataSource[section];\
    if (group.headerView)\
    {\
        return group.headerView;\
    }\
    else if (group.headerViewClass)\
    {\
        return [group.headerViewClass new];\
    }\
    else\
    {\
        return nil;\
    }\
}\
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section\
{\
    RBGroupModel *group = dataSource[section];\
    return group.headerHeight;\
}\
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section\
{\
    RBGroupModel *group = dataSource[section];\
    if (group.footerView)\
    {\
        return group.footerView;\
    }\
    else if (group.footerViewClass)\
    {\
    return [group.footerViewClass new];\
    }\
    else\
    {\
    return nil;\
    }\
}\
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section\
{\
    RBGroupModel *group = dataSource[section];\
    return group.footerHeight;\
}\
